<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="关键词" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入关键词"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布人员" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入发布人员"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
          >发送短信</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <div>
      <el-checkbox-group v-model="ids" @change="handleSelectionChange">
        <el-card
          v-for="item in noticeList"
          :key="item.id"
          class="margin-content"
        >
          <template #header>
            <el-row>
              <el-col :span="12">
                <el-checkbox :label="item.id">
                <div></div></el-checkbox>
              </el-col>
              <el-col :span="12" class="text-right" style="line-height: 32px">
                <el-link
                  :underline="false"
                  type="info"
                  @click="handleDelete(item.id)"
                  >删除</el-link
                ></el-col
              >
            </el-row>
          </template>
          <div style="font-size: 14px;">{{ item.content }}</div>
        </el-card>
      </el-checkbox-group>
    </div>
    <el-row v-show="total > 0">
      <el-col :span="12">
        <span style="padding-right: 12px">
          <el-checkbox
            v-model="checkAll"
            :indeterminate="isIndeterminate"
            @change="handleCheckAllChange"
            >全选</el-checkbox
          >
        </span>
        <el-button
          type="danger"
          plain
          :disabled="ids.length == 0"
          @click="handleBatchDelete"
          v-hasPermi="['system:notice:add']"
          >批量删除</el-button
        >
      </el-col>
      <el-col :span="12">
        <pagination
          :total="total"
          v-model:page="queryParams.page_size"
          v-model:limit="queryParams.pageCount"
          @pagination="getList"
      /></el-col>
    </el-row>

    <!-- 添加 -->
    <el-dialog :title="title" v-model="open" width="560px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="发送对象" prop="receiver">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="输入用户手机号码，多个号码请用逗号隔开"
                v-model="form.receiver"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="短信内容" prop="content">
              <el-input
                :rows="6"
                maxlength="200"
                show-word-limit
                type="textarea"
                placeholder="请输入短信内容"
                v-model="form.content"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Notice">
import { list, add, detail, del, batchDel } from "@/api/business/short-message";

const { proxy } = getCurrentInstance();
const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref( []);
const total = ref(0);
const title = ref("");
const checkAll = ref(false);
const isIndeterminate = ref(false);

const handleCheckAllChange = (val) => {
  let idList = noticeList.value.map((item) => item.id);
  ids.value = val ? idList : [];
  isIndeterminate.value = false;
};
const handleSelectionChange = (value) => {
  let idList = noticeList.value.map((item) => item.id);
  const checkedCount = value.length;
  checkAll.value = checkedCount === idList.length;
  isIndeterminate.value = checkedCount > 0 && checkedCount < idList.length;
};

const data = reactive({
  form: {},
  queryParams: {
    pageCount: 10,
    pageSize: 1,
    title: "",
    creator: "",
    createTime: "",
  },
  rules: {
    receiver: [
      { required: true, message: "发送对象不能为空", trigger: "blur" },
    ],
    title: [{ required: true, message: "短信标题不能为空", trigger: "blur" }],
    content: [{ required: true, message: "短信类型不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

// 统计发送对象数量
watch(
  () => data.form.receiver,
  (newValue, oldValue) => {
    data.form.receNum = newValue.length > 0 ? newValue.split(",").length : 0;
  }
);

/** 查询短信列表 */
function getList() {
  loading.value = true;
  list(queryParams.value).then((response) => {
    noticeList.value = response.data.list
    total.value = response.data.total_page;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    receiver: "",
    content: "",
    receNum: "0",
  };
  proxy.resetForm("noticeRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageCount = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加短信";
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      add(form.value).then((response) => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}
/** 删除按钮操作 */
function handleDelete(id) {
  proxy.$modal
    .confirm('是否确认删除短信编号为"' + id + '"的数据项？')
    .then(function () {
      return del(id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 批量删除 */
function handleBatchDelete() {
  const data = ids.value;
  proxy.$modal
    .confirm('是否确认删除短信编号为"' + data.join(",") + '"的数据项？')
    .then(function () {
      return batchDel({ ids: data });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
